package org.giovanolli.mumi.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.giovanolli.mumi.dao.RegionDao;
import org.giovanolli.mumi.entities.Region;
import org.giovanolli.mumi.entities.filter.EntityFilter;
import org.giovanolli.mumi.entities.filter.RegionFilter;
import org.giovanolli.mumi.entities.filter.SortCriteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * The Class RegionDaoImpl.
 */
@Repository("regionDao")
public class RegionDaoImpl extends HibernateDao<Region> implements RegionDao {

	@Override
	protected DetachedCriteria fillCriteria(EntityFilter<Region> filter, DetachedCriteria criteria) {
		final RegionFilter entityFilter = (RegionFilter) filter;

		if (filter != null) {
			if (StringUtils.isNotBlank(entityFilter.getName())) {
				criteria.add(Restrictions.ilike("name", entityFilter.getName(), MatchMode.ANYWHERE));
			}
		}
		return criteria;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.dao.impl.HibernateDao#setDefaultSortCriteria(java.util.List)
	 */
	@Override
	protected void setDefaultSortCriteria(List<SortCriteria> defaultSortCriteria) {
		defaultSortCriteria.add(new SortCriteria("name", true));
	}
}
